/* @Description:midway-back/src/modelindex.ts @author:378237242@qq.com @date:2023/11/20 */

import { globSync } from "glob";
import { createRequire } from "module";
// import { loggers } from "@midwayjs/logger";
import path from "path";

// const logger = loggers.createLogger("logger", {});
const mRequire = createRequire(__filename);

const importModules = () => {
  const modulesPath = globSync(__dirname + "/*/index.*").map((exportPath) => {
    const t = exportPath.replace(/src\\model/, "");
    return path.join(__dirname, t);
  });
  // const modulesPath = globSync(__dirname + "/*/index.*");
  // logger.info(__dirname);
  // logger.info("modulesPath.globSync.modulesPath:", modulesPath);
  const modules = [];

  for (const file of modulesPath) {
    try {
      const module = mRequire(file);
      modules.push(module);
    } catch (error) {
      // logger.error(`Failed to import module ${file}:`, error);
      return [];
    }
  }

  return modules;
};

const modules = importModules();

export default modules;
